package com.goldsprite.gameframeworks.rhino;

import org.mozilla.javascript.*;
import com.goldsprite.gameframeworks.*;

public class JsEngine {

	public static String eval(String js) {
		String result = "eval error.";

		try {
			Context rhinoCtx = Context.enter();

			rhinoCtx.setOptimizationLevel(-1);
			Scriptable scope = rhinoCtx.initStandardObjects();


			scope.put("Runnable", scope, Context.javaToJS(Runnable.class, scope));
			{
				Object resultObj = rhinoCtx.evaluateString(scope, js, "eval_js_string", 1, null);
				result = Context.toString(resultObj);
			}
		}
		catch(Exception e) {
			result = "脚本执行失败: 错误: \n"+e.toString();
		}
		finally {
			Context.exit();
		}
		return result;
	}

//	public static void androidEval(String jsCode) {
//		android.content.Context androidCtx = PlatformImpl.getAndroidCtx();
//		if (androidCtx != null) {
//			// 使用 RhinoAndroidHelper 初始化
//			RhinoAndroidHelper rhinoHelper = new RhinoAndroidHelper(androidCtx);
//			Context rhinoContext = rhinoHelper.enterContext();
//		}
//	}

}
